﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Gmantis.Controls.Chart
{
    [Obsolete("Please use PieSlice class instead.")]
    public class Pie : PlotElement
    {
        // Fields
        private Rect _lrect = new Rect();
        internal PieInfo _pi = new PieInfo();
        internal PathGeometry geometry;
        private Point offset0 = new Point();

        // Methods
        public Pie()
        {
            PathGeometry geometry = new PathGeometry
            {
                Figures = new PathFigureCollection()
            };
            this.geometry = geometry;
            ((Path)base.Shape).Data = this.geometry;
            base.StrokeLineJoin = PenLineJoin.Round;
        }

        protected override LabelAlignment AutoPosition(Size labelSize, ref Point hot, ref Point offset)
        {
            hot = this._pi.GetRadiusCenter();
            this._pi.RadiusX += offset.X;
            this._pi.RadiusY += offset.X;
            Point radiusCenter = this._pi.GetRadiusCenter();
            this._pi.RadiusX -= offset.X;
            this._pi.RadiusY -= offset.X;
            offset = new Point(radiusCenter.X - hot.X, radiusCenter.Y - hot.Y);
            if (offset.X == 0.0)
            {
                double num = this._pi.Angle + (0.5 * this._pi.Sweep);
                switch ((int)num)
                {
                    case 0:
                    case 360:
                        return LabelAlignment.MiddleRight;
                }
                if ((num > 0.0) && (num < 90.0))
                {
                    return LabelAlignment.BottomRight;
                }
                if (num == 90.0)
                {
                    return LabelAlignment.BottomCenter;
                }
                if ((num > 90.0) && (num < 180.0))
                {
                    return LabelAlignment.BottomLeft;
                }
                if (num == 180.0)
                {
                    return LabelAlignment.MiddleLeft;
                }
                if ((num > 180.0) && (num < 270.0))
                {
                    return LabelAlignment.TopLeft;
                }
                if (num == 270.0)
                {
                    return LabelAlignment.TopCenter;
                }
                if ((num > 270.0) && (num < 360.0))
                {
                    return LabelAlignment.TopRight;
                }
            }
            return LabelAlignment.MiddleCenter;
        }

        internal override object Clone()
        {
            Pie clone = new Pie();
            base.CloneAttributes(clone);
            return clone;
        }

        internal static void CreateFullPieWithHole(PathGeometry pg, PieInfo pi)
        {
            Point point = new Point();
            double radiusX = pi.RadiusX;
            double radiusY = pi.RadiusY;
            double width = radiusX * pi.InnerRadius;
            double height = radiusY * pi.InnerRadius;
            double angle = pi.Angle;
            double sweep = pi.Sweep;
            double d = angle * 0.017453292519943295;
            double num8 = (angle + 359.99999) * 0.017453292519943295;
            double num9 = Math.Cos(d);
            double num10 = Math.Sin(d);
            double num11 = Math.Cos(num8);
            double num12 = Math.Sin(num8);
            Point point2 = new Point(point.X + (radiusX * num9), point.Y + (radiusY * num10));
            Point point3 = new Point(point.X + (radiusX * num11), point.Y + (radiusY * num12));
            Point point4 = new Point(point.X + (width * num11), point.Y + (height * num12));
            Point point5 = new Point(point.X + (width * num9), point.Y + (height * num10));
            PathFigure figure = new PathFigure
            {
                StartPoint = point2,
                IsClosed = true,
                Segments = new PathSegmentCollection(),
                IsFilled = true
            };
            ArcSegment segment = new ArcSegment
            {
                Point = point3,
                Size = new Size(radiusX, radiusY),
                IsLargeArc = sweep > 180.0,
                SweepDirection = SweepDirection.Clockwise
            };
            figure.Segments.Add(segment);
            pg.Figures.Add(figure);
            figure = new PathFigure
            {
                StartPoint = point4,
                IsClosed = true,
                Segments = new PathSegmentCollection(),
                IsFilled = true
            };
            ArcSegment segment2 = new ArcSegment
            {
                Point = point5,
                Size = new Size(width, height),
                IsLargeArc = sweep > 180.0,
                SweepDirection = SweepDirection.Counterclockwise
            };
            figure.Segments.Add(segment2);
            pg.Figures.Add(figure);
        }

        internal static PathFigure CreatePie(PieInfo pi)
        {
            Point point = new Point();
            double angle = pi.Angle;
            double radiusX = pi.RadiusX;
            double radiusY = pi.RadiusY;
            double sweep = pi.Sweep;
            bool flag = sweep == 360.0;
            if (sweep == 360.0)
            {
                sweep = 359.99999;
            }
            double d = angle * 0.017453292519943295;
            double num6 = (angle + sweep) * 0.017453292519943295;
            double num7 = Math.Cos(d);
            double num8 = Math.Sin(d);
            double num9 = Math.Cos(num6);
            double num10 = Math.Sin(num6);
            Point point2 = new Point(point.X + (radiusX * num7), point.Y + (radiusY * num8));
            Point point3 = new Point(point.X + (radiusX * num9), point.Y + (radiusY * num10));
            PathFigure figure = new PathFigure
            {
                StartPoint = point,
                IsClosed = true,
                Segments = new PathSegmentCollection()
            };
            LineSegment segment = new LineSegment
            {
                Point = point2
            };
            ArcSegment segment2 = new ArcSegment
            {
                Point = point3
            };
            figure.IsFilled = true;
            segment2.Size = new Size(radiusX, radiusY);
            segment2.IsLargeArc = sweep > 180.0;
            segment2.SweepDirection = SweepDirection.Clockwise;
            LineSegment segment3 = new LineSegment
            {
                Point = point
            };
            if (!flag)
            {
                figure.Segments.Add(segment);
            }
            else
            {
                figure.StartPoint = point2;
            }
            figure.Segments.Add(segment2);
            if (!flag)
            {
                figure.Segments.Add(segment3);
            }
            return figure;
        }

        private static PathFigure CreatePieWithHole(PieInfo pi)
        {
            Point point = new Point();
            double radiusX = pi.RadiusX;
            double radiusY = pi.RadiusY;
            double width = radiusX * pi.InnerRadius;
            double height = radiusY * pi.InnerRadius;
            double angle = pi.Angle;
            double sweep = pi.Sweep;
            if (sweep == 360.0)
            {
                sweep = 359.99999;
            }
            double d = angle * 0.017453292519943295;
            double num8 = (angle + sweep) * 0.017453292519943295;
            double num9 = Math.Cos(d);
            double num10 = Math.Sin(d);
            double num11 = Math.Cos(num8);
            double num12 = Math.Sin(num8);
            Point point2 = new Point(point.X + (radiusX * num9), point.Y + (radiusY * num10));
            Point point3 = new Point(point.X + (radiusX * num11), point.Y + (radiusY * num12));
            Point point4 = new Point(point.X + (width * num11), point.Y + (height * num12));
            Point point5 = new Point(point.X + (width * num9), point.Y + (height * num10));
            PathFigure figure = new PathFigure
            {
                StartPoint = point5,
                IsClosed = true,
                Segments = new PathSegmentCollection()
            };
            LineSegment segment = new LineSegment
            {
                Point = point2
            };
            ArcSegment segment2 = new ArcSegment
            {
                Point = point3
            };
            figure.IsFilled = true;
            segment2.Size = new Size(radiusX, radiusY);
            segment2.IsLargeArc = sweep > 180.0;
            segment2.SweepDirection = SweepDirection.Clockwise;
            LineSegment segment3 = new LineSegment
            {
                Point = point4
            };
            ArcSegment segment4 = new ArcSegment
            {
                Point = point5,
                Size = new Size(width, height),
                IsLargeArc = sweep > 180.0,
                SweepDirection = SweepDirection.Counterclockwise
            };
            figure.Segments.Add(segment);
            figure.Segments.Add(segment2);
            figure.Segments.Add(segment3);
            figure.Segments.Add(segment4);
            return figure;
        }

        protected override bool IsCompatible(IRenderer rend)
        {
            return (rend is PieRenderer);
        }

        protected override bool Render(RenderContext rc)
        {
            PieRenderContext context = rc as PieRenderContext;
            if ((((context == null) || double.IsNaN(context.PieInfo.Center.X)) || (double.IsNaN(context.PieInfo.Center.Y) || (context.PieInfo.RadiusX <= 0.0))) || (context.PieInfo.RadiusY <= 0.0))
            {
                return false;
            }
            PathGeometry pg = this.geometry;
            PathFigure figure = null;
            PieInfo pieInfo = context.PieInfo;
            if ((pieInfo.InnerRadius > 0.0) && (pieInfo.Sweep == 360.0))
            {
                CreateFullPieWithHole(pg, pieInfo);
            }
            else
            {
                figure = (context.PieInfo.InnerRadius > 0.0) ? CreatePieWithHole(pieInfo) : CreatePie(pieInfo);
                pg.Figures.Add(figure);
            }
            Point radiusCenter = pieInfo.GetRadiusCenter();
            this._lrect.X = radiusCenter.X;
            this._lrect.Y = radiusCenter.Y;
            this._pi = pieInfo;
            TranslateTransform transform = new TranslateTransform
            {
                X = this._pi.Center.X,
                Y = this._pi.Center.Y
            };
            this.geometry.Transform = transform;
            this.offset0 = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
            this.UpdateOffset();
            return true;
        }

        protected void UpdateOffset()
        {
            Point point = new Point();
            if (this._pi.Offset != 0.0)
            {
                double d = 0.017453292519943295 * (this._pi.Angle + (this._pi.Sweep * 0.5));
                point.X = this._pi.Offset * Math.Cos(d);
                point.Y = this._pi.Offset * Math.Sin(d);
            }
            Canvas.SetLeft(this, this.offset0.X + point.X);
            Canvas.SetTop(this, this.offset0.Y + point.Y);
            if ((base.effects != null) && (base.Shape != null))
            {
                foreach (UIElement element in base.effects)
                {
                    Canvas.SetLeft(element, point.X + this.offset0.X);
                    Canvas.SetTop(element, point.Y + this.offset0.Y);
                }
            }
        }

        // Properties
        internal override Rect LabelRect
        {
            get
            {
                return this._lrect;
            }
        }
    }
}
